// pages/dministratorsLogin/dministratorsLogin.js
var app = getApp();
var util = require('../../utils/util');
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		ispwd:false,
		ischecked: false
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {

	},
	detilxieyiTap:function(e){
		wx.navigateTo({
			url: '../dministratorsduty/dministratorsduty?type=2',
			success: function(res) {},
			fail: function(res) {},
			complete: function(res) {},
		})
	},
	formSubmit: function (e) {
		var that = this;
		// 查看是否授权
		var username = e.detail.value.username;
		if (username.length < 1) {
			util.showSuccess('请输入账号', 'none', false);
			return false;
		}
		var password = e.detail.value.password;
		if (password.length < 1) {
			util.showSuccess('请输入密码', 'none', false);
			return false;
		}
		if (that.data.ischecked==false){
			util.showSuccess('请选中主管协议', 'none', false);
			return false;
		}
		// 登录
		wx.login({
			success: res => {
				// 发送 res.code 到后台换取 openId, sessionKey, unionId
				console.log(res);
				util.showLoading();
				var apiUrl = '/api/director/login/index_wx';
				var apiData = {
					js_code: res.code,
					username: username,
					password: password,
				}
				util.http(apiUrl, apiData, function (res) {
					util.hideLoading();
					if (res.state == 1) {
						wx.setStorageSync('openid', {
							name: res.datas.name,
							openid:res.datas.openid,
							image: res.datas.image,
							school_name: res.datas.school_name,
							dir_id:res.datas.id
						})
						util.showSuccess('登录成功');
						setTimeout(function () {
							wx.redirectTo({
								url: "../dministratorsIndex/dministratorsIndex",
							})
						}, 2000);
					} else {
						util.message(res.datas);
					}
				});
			}
		})
	},
	//用户协议
	xieyiChecked:function(e){
		console.log("sdkljkl")
		this.data.ischecked = !this.data.ischecked;
		this.setData({
			ischecked: this.data.ischecked
		})
	},
		
	//是否记住密码
	pwdChecked: function (e) {
		this.data.ispwd = !this.data.ispwd;
		if (this.data.ispwd) {
			wx.setStorageSync("pwdInfo", {
				username: this.data.username,
				password: this.data.password
			})
		} else {
			wx.removeStorage({
				key: 'pwdInfo',
				success(res) {
					console.log(res)
				}
			})
		}
		this.setData({
			ispwd: this.data.ispwd
		})
		//   console.log(this.data.username, password);
	},

	//是否记住密码  使用
	onUserName: function (e) {
		this.setData({
			username: e.detail.value
		})
	},
	//是否记住密码  使用
	onPassWord: function (e) {
		this.setData({
			password: e.detail.value
		})
	},
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function () {

	}
})